<template>
	<view class="bargain-record">
		<view class="bargain-record-item" v-for="(item,index) in list" :key="item.id">
			<bargain-record-item :item="item"></bargain-record-item>
		</view>
		<uni-load-more :status="status" :contentText="contentText" @tap="loadList"></uni-load-more>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
	import bargainRecordItem from '@/components/home/bargain-record-item/index.vue'
	let gData = {};
	let _self;
	export default {
		components: {
			uniLoadMore,
			bargainRecordItem
		},
		data() {
			return {
				bargainPartakeId:null,
				list: [],
				total: 0,
				status: "more",
				contentText: {
					contentdown: "点击加载",
					contentrefresh: "正在加载...",
					contentnomore: "已无更多"
				}
			}
		},
		onLoad(options) {
			_self = this;
			this.bargainPartakeId=options.bargainPartakeId;
			this.initList()
		},
		onUnload() {
			gData = {}; /*因为全局变量会保存，再次打开该页面时会造成一些数据错误 */
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.initList();
			uni.stopPullDownRefresh()
		},
		// 上拉加载
		onReachBottom() {
			this.loadList();
		},
		methods: {
			// 初始化列表
			initList() {
				gData.page = 0;
				this.list = [];
				this.status = "more";
				this.loadList();
			},
			// 加载列表
			async loadList() {
				if (this.status == "more") {
					const page = gData.page + 1;
					this.status = "loading";
					const pageSize = 10;
					// uni.showNavigationBarLoading();
					try {
						let data = await this.$api.bargain.getBargainRecordList({
							id:this.bargainPartakeId,
							page: page, // 页数
							rows: pageSize //
						});
						console.log(data)
						
						const addList = data.list || [];
						addList.map((item) => {
							if (item.lotteryTime) {
								const arr = this.$util.formatTimestamp(item.lotteryTime)
								item.lotteryTime = arr[1] + '月' + arr[2] + '日 ' + arr[3] + ':' + arr[4];
							}
						})
						this.list = this.list.concat(addList)
						gData.page = page;
						this.total = data.total;
						const noMore = (page * pageSize) >= data.total;
						this.status = noMore ? "noMore" : "more";
						// uni.hideNavigationBarLoading()
					} catch (e) {
						this.status = "more";
						console.log('request fail', e);
					}
				};
			},
			toDrawDetails(id){
				uni.navigateTo({
					url:'/pages/draw/details/details?prizeId='+id
				})
			},
			toDraw(){
				uni.switchTab({
					url:'/pages/tabBar/draw/index'
				})
			},
			// ---------------------------------------
			// 保存二维码
			setFormId(e) {
				// _self.$api.setFormId(e);
			},
		},
	}
</script>


<style lang="scss" scoped>
	.bargain-record {
		position: relative;
		background-color: white;
		border-radius: 10rpx;
		overflow: hidden;

		.bargain-record-item {
			position: relative;
			padding: 30rpx 40rpx;

			&:before {
				content: ' ';
				position: absolute;
				bottom: 0;
				left: 30rpx;
				right: 30rpx;
				border-bottom: 1px solid #e5e5e5;
				opacity: 0.6;
			}

			&:last-child:before {
				right: 0;
			}
		}
	}
</style>
